<?php
/*
 * @titulo: listado_server.php
 */

if (!defined('RAIZ')) define('RAIZ', $_SERVER['DOCUMENT_ROOT']. '/neumo/'); 

require_once RAIZ . 'lib/main.php';
require_once RAIZ . 'db/db_mysql.php';
require_once RAIZ . 'liblnk/lib/eloy/auxDb.php';

$SQL_path= RAIZ . 'db/mysql.sql';

$page = $_POST['page']; // get the requested page
$limit = $_POST['rows']; // get how many rows we want to have into the grid
$sidx = $_POST['sidx']; // get index row - i.e. user click to sort
$sord = $_POST['sord']; // get the direction
$bsc_palabra = isset($_POST['searchString']) ? $_POST['searchString'] : ''; 
$bsc_campo= isset($_POST['searchField']) ? $_POST['searchField'] : ''; 
$bsc_Oper= isset($_POST['searchOper']) ? $_POST['searchOper'] : ''; 


(isset($_GET['tipo']))or die('Error no se ha definido el GET de tipo');
$tipo= $_GET['tipo'];

//objeto_crea_filtro($tipo,$busca,$borrador,$aprobado, $perfil, $publicacion, $nopublicacion)
$filtro= objeto_crea_filtro($tipo, 0 ,0, 1, 1, 1, 1);

$responce = new stdClass;
if (!$sidx) $sidx = 1;
$db_objeto= new dbase_objeto;
$sql= GetSentenciaPrm($SQL_path, 'S_LISTADO', array('(1)','(1=1)','(ORDER)'), array('COUNT(*) as count', $filtro ,'')); 

$result = mysql_query($sql);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$count = $row['count'];

if ($count > 0) {
  $total_pages = ceil($count / $limit);
} else {
  $total_pages = 0;
}
if ($page > $total_pages) $page = $total_pages;

$start = $limit * $page - $limit; // do not put $limit*($page - 1)
$start= max($start,0);
$filtro.= ' AND ' . convierte_operador_jquery($bsc_campo,$bsc_Oper,$bsc_palabra);

$sql_limite= $start . ", " . $limit;
$sql_order=  $sidx . " ". $sord;

$sql= GetSentenciaPrm($SQL_path, 'S_LISTADO', array('(1=1)','(ORDER)'), array( $filtro, 
    ' ORDER BY ' . $sql_order . " LIMIT " . $sql_limite ));

$result = mysql_query($sql) or die("Error en la SQL de objetos" . mysql_error());

$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;

$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  $responce->rows[$i]['id'] = $row['id_objeto'];
  $responce->rows[$i]['cell'] = array($row['id_objeto'],$row['ffecha_publicacion'], $row['titulo'], str_replace(array(chr(10),chr(13)),array(''),$row['comentario']));
  $i++;
}

$db_objeto->cierra();

echo json_encode($responce);


/*==============================================================================
 * Subrutinas
 */


?>
